﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sunboxsoft.ASMS.Server.Domain.Server
{
    class ServerData
    {
        private static ServerData s_instance;
        public static ServerData Instance
        {
            get
            {
                if (s_instance == null)
                {
                    s_instance = new ServerData();
                }
                return s_instance;
            }
        }

        public static readonly object BookingBillLockObject = new object();

        public string BeginNewBillSeedString(ASMSDbContext dbContext, string header)
        {
            Entity.ESeed seed = dbContext.Set<Entity.ESeed>().SingleOrDefault(p => p.Key == header);
            seed.Value++;
            dbContext.Entry<Entity.ESeed>(seed).State = System.Data.Entity.EntityState.Modified;
            return string.Format("{0}{1}", header, seed.Value);
        }

        public void CancelNewBillSeed(string header)
        {
        }

        private List<SyncServiceItemActionLock> _syncServiceItemActionLockList = new List<SyncServiceItemActionLock>();

        public bool HasSyncServiceitemActionLock(Guid businessId, Guid? stationId)
        {
            for (int index = 0; index < _syncServiceItemActionLockList.Count; index++)
            {
                if (_syncServiceItemActionLockList[index].BusinessId == businessId && _syncServiceItemActionLockList[index].StationId == stationId)
                {
                    return true;
                }
            }
            return false;
        }
        public bool CreateSyncServiceItemActionLock(Guid businessId, Guid? stationId)
        {
            for (int index = 0; index < _syncServiceItemActionLockList.Count; index++)
            {
                if (_syncServiceItemActionLockList[index].BusinessId == businessId && _syncServiceItemActionLockList[index].StationId == stationId)
                {
                    return false;
                }
            }
            SyncServiceItemActionLock @lock = new SyncServiceItemActionLock();
            @lock.BusinessId = businessId;
            @lock.StationId = stationId;
            _syncServiceItemActionLockList.Add(@lock);
            return true;
        }

        public void DeleteSyncServiceItemActionLock(Guid businessId, Guid? stationId)
        {
            for (int index = 0; index < _syncServiceItemActionLockList.Count; index++)
            {
                if (_syncServiceItemActionLockList[index].BusinessId == businessId && _syncServiceItemActionLockList[index].StationId == stationId)
                {
                    _syncServiceItemActionLockList.RemoveAt(index);
                    return;
                }
            }
        }
    }
}
